<template>
  <div>
    <!-- Breadcrumb 面包屑 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>科室管理</el-breadcrumb-item>
      <el-breadcrumb-item>科室列表</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 搜索 -->
    <el-row :gutter="20">
      <el-col :span="12">
        <el-input placeholder="科室名模糊查询" v-model="queryInfo.query">
          <el-button
            slot="append"
            icon="el-icon-search"
            @click="getDeprtList"
          ></el-button>
        </el-input>
      </el-col>
      <el-col :span="4">
        <el-button type="primary" @click="depDialogVisible = true"
          >新增科室</el-button
        >
      </el-col>
      <el-col :span="4">
        <el-button type="success" @click="editDialogVisible = true"
          >查询科室</el-button
        >
      </el-col>
      <el-col :span="4">
        <el-button type="danger" @click="removeBatch(sels)">批量删除</el-button>
      </el-col>
    </el-row>

    <!-- card展示 -->
    <el-card>
      <el-table @selection-change="selsChange"   :data="departList" border stripe>
        <!-- 为表格添加一个编号（索引） -->
        <el-table-column type="selection" width="65"></el-table-column>
        <el-table-column type="index" label="编号"> </el-table-column>
        <el-table-column prop="deptcode" label="科室编码"></el-table-column>
        <el-table-column prop="deptname" label="科室名称"> </el-table-column>
        <el-table-column
          prop="deptcategoryid"
          label="科室分类"
         
        >
        </el-table-column>
        <el-table-column width="200px" label="操作">
          <template slot-scope="scope">
            <!-- 修改按钮 -->
            <el-button
              type="primary"
              icon="el-icon-edit"
              size="mini"
              @click="showEditDialog(scope.row.id)"
            ></el-button>
            <!-- 删除按钮 -->
            <el-tooltip
              effect="dark"
              content="删除信息"
              placement="top"
              :enterable="false"
            >
              <el-button
                type="danger"
                icon="el-icon-delete"
                size="mini"
                @click="removeDepById(scope.row.id)"
              ></el-button>
            </el-tooltip>
          </template>
        </el-table-column>
      </el-table>
         <!-- Pagination分页 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryInfo.pageNum"
        :page-sizes="[1, 2, 5, 10]"
        :page-size="queryInfo.pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </el-card>
    <el-dialog title="添加科室" :visible.sync="depDialogVisible" width="50%">
      <!-- 创建一个添加科室表单 -->
      <!-- 科室编码 科室名称   科室分类   所在科类-->
      <!-- 写完表单后要添加数据绑定以及检验的规则 -->
      <el-form :model="depForm" :rules="depFormRules" ref="depFormRef">
        <el-form-item prop="deptcode" label="科室编码">
          <el-input
            v-model="depForm.deptcode"
            prefix-icon="iconfont icon-user"
            placeholder="请填写科室编码"
          ></el-input>
        </el-form-item>
        <el-form-item prop="deptname" label="科室名称">
          <el-input
            v-model="depForm.deptname"
            prefix-icon="iconfont icon-user"
            placeholder="请填写科室名称"
          ></el-input>
        </el-form-item>
        <el-form-item prop="deptcategoryid" label="科室分类">
          <el-input
            v-model="depForm.deptcategoryid"
            prefix-icon="iconfont icon-user"
            placeholder="请填写科室分类"
          ></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button  @click="depDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="sureAdd">确 定</el-button>
      </span>
    </el-dialog>

    <!-- 查询用户的对话框 -->
    <el-dialog
      title="查询用户"
      :visible.sync="editDialogVisible"
      width="80%"
    >
      <el-form
:rules="qqFormRules" ref="qqFormRef"

        :model="queryForm"
        label-width="100px"
      >
        <el-form-item label="科室编码">
          <el-input v-model="queryForm.deptcode" prop="deptcode" ></el-input>
        </el-form-item>
        <el-form-item label="科室名称" prop="deptname">
          <el-input v-model="queryForm.deptname"></el-input>
        </el-form-item>
        <el-form-item label="科室分类" prop="deptcategoryid">
          <el-input v-model="queryForm.deptcategoryid"></el-input>
        </el-form-item>
        <el-form-item label="科室类型" prop="depttype">
          <el-input v-model="queryForm.depttype"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click=" editDialogVisible=false">取 消</el-button>
        <el-button type="primary" @click="queryUser">查询</el-button>
      </span>
    </el-dialog>

     <el-dialog title="修改用户" :visible.sync="updateDialogVisible" width="50%">
      <!-- 创建一个表单 -->
      <!-- 用户名 密码  真实姓名 用户类型 医生职称 用户所在科类-->
      <!-- 写完表单后要修改数据绑定以及检验的规则 -->
      <el-form :model="updateForm" :rules="updateFormRules" ref="updateFormRef">
        <el-form-item prop="deptcode" label="科室编码">
          <el-input
            v-model="updateForm.deptcode"
            prefix-icon="iconfont icon-user"
            placeholder="请填科室编码"
          ></el-input>
        </el-form-item>
        <el-form-item prop="deptname" label="科室名称">
          <el-input
            v-model="updateForm.deptname"
            prefix-icon="iconfont icon-user"
            placeholder="请输科室名称"
          ></el-input>
        </el-form-item>
        <el-form-item prop="deptcategoryid" label="科室分类">
          <el-input
            v-model="updateForm.deptcategoryid"
            prefix-icon="iconfont icon-user"
            placeholder="请输科室分类"
          ></el-input>
        </el-form-item>
        <el-form-item prop="depttype" label="科室类型">
          <el-input
            v-model="updateForm.depttype"
            prefix-icon="iconfont icon-user"
            placeholder="请输科室类型"
          ></el-input>
        </el-form-item>
       
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="updateDialogVisible=false">取 消</el-button>
        <el-button type="primary" @click="sureUpdate">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
    data(){
        return{
//查询所有科室列表信息
          total:0,
          queryInfo:{
          pageNum:1,
          pageSize:2
        },
//科室列表信息
        departList:[],
         sels:[],

//科室添加Dilog框默认为关闭
         depDialogVisible:false,
//科室查询Dilog框默认关闭
        editDialogVisible:false,
//修改查询Dilog框默认关闭        
        updateDialogVisible:false,

//添加信息表单
depForm:{
  deptcode:"",
  deptname:"",
  deptcategoryid:null,
},
//查询表单信息
queryForm:{
  deptcode:"",
  deptname:"",
  deptcategoryid:null,
  depttype:null,
},
//修改表单信息
updateForm:{
  id:null,
   deptcode:"",
  deptname:"",
  deptcategoryid:null,
  depttype:null,
},
 depFormRules:{
        deptcode:[
           { required: true, message: "不能为空", trigger: "blur" },
          ],
        deptname: [
          { required: true, message: "请输入密码", trigger: "blur" },
         
        ],
          deptcategoryid:[
            { required: true, message: "不能为空", trigger: "blur" },
         
         ],
        depttype:[  { required: true, message: "不能为空", trigger: "blur" }]
        
      },
      //修改信息表单验证
      updateFormRules:{
       deptcode:[
           { required: true, message: "不能为空", trigger: "blur" },
          ],
        deptname: [
          { required: true, message: "请输入密码", trigger: "blur" },
         
        ],
          deptcategoryid:[
            { required: true, message: "不能为空", trigger: "blur" },
         
         ],
        depttype:[  { required: true, message: "不能为空", trigger: "blur" }]
      },

       //修改信息表单验证
      qqFormRules:{
       deptcode:[
           { required: true, message: "不能为空", trigger: "blur" },
          ],
        deptname: [
          { required: true, message: "请输入密码", trigger: "blur" },
         
        ],
          deptcategoryid:[
            { required: true, message: "不能为空", trigger: "blur" },
         
         ],
        depttype:[  { required: true, message: "不能为空", trigger: "blur" }]
      },
        }
       
    },
     created() {
    this.getDeprtList();
  },
    methods:{
        //查询所有科室列表
       async getDeprtList(){
 const {data:res}=await this.$http.get(`department/queryDeptName`, { params: this.queryInfo});
      // 如果状态异常
      if (res.meta.status !== 200) {
        return this.$message.error("获取医生职称信息失败");
      }
    this.departList=res.list;
     // 如果状态正常，获取用户数据
      this.userList = res.list;
      this.total = res.total;
    console.log(res);
        },

        //添加信息 确定
   async sureAdd(){
     const {data:res}= await this.$http.post(`department/insertDep`, 
       this.depForm
      )
        // 如果状态异常
      if (res.meta.status !== 200) {
        return this.$message.error("添加用户信息失败");
      }
     this.$refs.addFormRef.resetFields();
       this.depDialogVisible=false;
        this.getDeprtList();
    },

//分页按钮功能
         handleSizeChange(size) {
      // console.log(size)
      this.queryInfo.pageSize = size;
      //重新查询科室信息
     this.getDeprtList();
    },
    handleCurrentChange(page) {
      // console.log(page)
      this.queryInfo.pageNum = page;
      //重新查询科室信息
      this.getDeprtList();
    },
//根据id删除科室信息
   //删除用户信息
    async removeDepById(row){
     const{data:res}=await this.$http.get(`department/deleteDepById`, {
        params: {id:row}
      })
       // 如果状态异常
      if (res.meta.status !== 200) {
        return this.$message.error("删除用户信息失败");
      }
     this.getDeprtList();
    },

      //根据条件查询用户信息
    async queryUser(){
     const { data: res } = await this.$http.post("department/queryDep",this.queryForm);
     //如果状态异常
      // 如果状态异常
      if (res.meta.status !== 200) {
        return this.$message.error("获取用户信息失败");
      }
       // 如果状态正常，获取用户数据
      this.departList = res.department;
       this.$refs.qqFormRef.resetFields();
      console.log(res);
       this.editDialogVisible=false
    },

      //批量删除 向后台发送请求
  async removeBatch(rows){
  // 弹框询问用户是否删除数据
      const confirmResult = await this.$confirm(
        "此操作将永久删除该用户, 是否继续?",
        "提示",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }
      ).catch(err => err);

      // 如果用户确认删除，则返回值为字符串 confirm
      // 如果用户取消了删除，则返回值为字符串 cancel
      // console.log(confirmResult)

      if (confirmResult !== "confirm") {
        return this.$message.info("已取消删除");
      }

    var ids = [];
    rows.forEach(element=>{
      ids.push(element.id)
    })

    const{data:res} =await this.$http.post(`department/deleteByChoose`,{info:ids});
     // 如果状态异常
      if (res.meta.status !== 200) {
        return this.$message.error("删除用户信息失败");
      }
      this.getDeprtList();
 },
      //批量删除 复选框 选中id放进数组
    selsChange(sels){
     this.sels=sels
    },

//修改对话框
   async showEditDialog(row){
        this.updateDialogVisible=true
       //根据id获取用户信息
      const{data:res}=await this.$http.get(`department/selectUserById`,{  params: {id:row}})
        // 如果状态异常
      if (res.meta.status !== 200) {
        return this.$message.error("根据id用户信息失败");}
        console.log(res)
             this.updateForm.deptcode=res.department.deptcode
             this.updateForm.deptname=res.department.deptname
             this.updateForm.deptcategoryid=res.department.deptcategoryid
             this.updateForm.depttype=res.department.depttype
              this.updateForm.id=row
       },
//确定修改科室信息
async sureUpdate(){
     const{data:res}=await this.$http.post(`department/updateUser`,  this.updateForm);
       if (res.meta.status !== 200) {
        return this.$message.error("修改用户信息失败");
      }
       this.$refs.updateFormRef.resetFields();
          this.getDeprtList();
          this.updateDialogVisible=false;
       }

    }
}
</script>

<style lang="less" scoped></style>
